var jwt = require("../utils/jwt")
var {match,uuid} = require("../utils/common")
var user = require("../service/user")
var group = require("../service/group")


const multer = require('multer')
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, './images')
    },
    filename: function (req, file, cb) {
        cb(null, uuid() + "-" + file.originalname)
    }
})
const upload = multer({ storage })

var bodyParser = require('body-parser');//用于req.body获取值的


module.exports = function (app) {

    passdUrls = ["/signin", "/signout", "/login", "/ws" , "/images/*"]

    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    app.use(function (req, res, next) {
        if (match(req.path, passdUrls)) {
            next()
        } else {
            if (req.headers.token === undefined) {
                res.send("请登录")
            } else {
                try {
                    jwt.verifyToken(req.headers.token)
                    next()
                } catch {
                    res.send("请登录")
                }

            }
        }
    })

    app.get("/hello", function (req, res) {
        res.send("hello word!")
    })

    app.post("/signin", function (req, res) {
        user.signin(req, res)
    })


    app.post("/login", function (req, res) {
        user.login(req, res)
    })

    app.post("/msg", function (req, res) {
        user.handleMsg()
    })

    //获取我好友的信息
    app.get("/friends", function (req, res) {
        // console.log(req.body)
        user.handleFriendsInfo(req,res)
    })

    //获取我所在组的信息
    app.get("/groups", function (req, res) {
        group.handleGroupsInfo(req, res)
    })

    app.post("/file_upload", upload.single('file'), function (req, res, next) {
        const file = req.file;
        res.send(file)
    })


}
